﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface;
using BusinessObjects;
using System.Data;
using DataObjects.AdoNet;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantMenuCategoryDao : IRestaurantMenuCategoryDao
    {
        public List<RestaurantMenuCategory> GetRestaurantMenuCategories()
        {
            string procedures = "Get_All_RestaurantMenuCategories";
            return Db.ReadList(procedures, true, Make);
        }
        public List<RestaurantMenuCategory> GetRestaurantMenuCategoriesByBranch(string restaurantId)
        {
            string procedures = "Get_All_RestaurantMenuCategories_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public RestaurantMenuCategory GetRestaurantMenuCategory(string restaurantMenuCategoryId)
        {
            string procedures = "Get_All_RestaurantMenuCategories_ByCondition";

            object[] parms = { "@WhereClause", " WHERE MenuCategoryID = '" + restaurantMenuCategoryId + "'" };
            return Db.Read(procedures, true, Make, parms);
        }
        public List<RestaurantMenuCategory> GetRestaurantMenuCategoriesActiveByBranch(bool isActive, string restaurantId)
        {
            string procedures = "Get_All_RestaurantMenuCategories_ByCondition";
            string active = null;

            if (isActive == true) { active = "1"; }
            else { active = "0"; }
            object[] parms = { "@WhereClause", " WHERE IsActive = " + active + " AND RestaurantID = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public int GetCountRestaurantMenuCategoryByParentId(string parentId)
        {
            string procedures = "Get_All_RestaurantMenuCategories_ByCondition";

            object[] parms = { "@WhereClause", " WHERE ParentID = '" + parentId + "'" };
            var restaurantMenuCategories = Db.ReadList(procedures, true, Make, parms);
            if (restaurantMenuCategories != null && restaurantMenuCategories.Count > 0)
                return restaurantMenuCategories.Count;
            else 
                return 0;
        }
        public void InsertRestaurantMenuCategory(RestaurantMenuCategory restaurantMenuCategory)
        {
            string sql = "Insert_RestaurantMenuCategory";

            restaurantMenuCategory.MenuCategoryID = Db.Insert(sql, true, Take(restaurantMenuCategory));
        }
        public void UpdateRestaurantMenuCategory(RestaurantMenuCategory restaurantMenuCategory)
        {
            string sql = "Update_RestaurantMenuCategory";

            Db.Update(sql, true, Take(restaurantMenuCategory));
        }
        public void DeleteRestaurantMenuCategory(RestaurantMenuCategory restaurantMenuCategory)
        {
            string sql = "Delete_RestaurantMenuCategory";

            Db.Update(sql, true, Take(restaurantMenuCategory.MenuCategoryID));
        }

        private object[] Take(string restaurantMenuCategoryId)
        {
            return new object[]  
            {
                "@MenuCategoryID", restaurantMenuCategoryId,
            };
        }

        private object[] Take(RestaurantMenuCategory restaurantMenuCategory)
        {
            return new object[]  
            {
                "@MenuCategoryID", restaurantMenuCategory.MenuCategoryID,
                "@MenuCategoryCode", restaurantMenuCategory.MenuCategoryCode,
                "@MenuCategoryName", restaurantMenuCategory.MenuCategoryName,
                "@ParentID", restaurantMenuCategory.ParentID,
                "@IsParent", restaurantMenuCategory.IsParent,
			    "@RestaurantID", restaurantMenuCategory.RestaurantID,
                "@Description", restaurantMenuCategory.Description ,
                "@IsActive", restaurantMenuCategory.IsActive
            };
        }

        private static Func<IDataReader, RestaurantMenuCategory> Make = reader =>
            new RestaurantMenuCategory
            {
                MenuCategoryID = reader["MenuCategoryID"].AsString(),
                MenuCategoryCode = reader["MenuCategoryCode"].AsString(),
                MenuCategoryName = reader["MenuCategoryName"].AsString(),
                ParentID = reader["ParentID"].AsString(),
                IsParent = reader["IsParent"].AsBool(),
                RestaurantID = reader["RestaurantID"].AsString(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
